<template>
  <div class="sidebar-msg-fixtop">
    <el-input
      prefix-icon="el-icon-search"
      size="small"
      placeholder="搜索通讯录"
      style="background-color: #ebebf2"
      v-model="value"
      clearable
      @blur="handleBlur()"
      @input="handleSearchByRealName"
    />

    <div class="search-list" :style="searchResult ? { height: '400px' } : ''">
      <!-- <i class="el-icon-loading" v-show="loading"></i>
      <div v-show="isNull" class="is-null">暂无数据</div> -->
      <div
        class="search-list-title"
        v-show="searchList.accountList && searchList.accountList.length > 0"
      >
        联系人
      </div>
      <div
        v-for="(item, index) in searchList.accountList || []"
        :key="index"
        @click="handleSelect(item, 1)"
        class="search-list-item"
      >
        <div class="item-avatar">
          {{ item.realName && item.realName.substr(-2) }}
        </div>
        <div>{{ item.realName }}</div>
      </div>
      <div
        class="search-list-title"
        v-show="searchList.groupList && searchList.groupList.length > 0"
      >
        群聊
      </div>
      <div
        v-for="(item, index) in searchList.groupList || []"
        :key="index + 'g'"
        @click="handleSelect(item, 2)"
        class="search-list-item"
      >
        <div class="item-avatar">群聊</div>
        <div>{{ item.name }}</div>
      </div>
      <div
        class="search-list-title"
        v-show="searchList.userGroupList && searchList.userGroupList.length > 0"
      >
        自定义群聊
      </div>
      <div
        v-for="(item, index) in searchList.userGroupList || []"
        :key="index + 'u'"
        @click="handleSelect(item, 3)"
        class="search-list-item"
      >
        <div class="item-avatar">群聊</div>
        <div>{{ item.name }}</div>
      </div>
      <!-- <div>暂无</div> -->
    </div>
  </div>
</template>

<script>
export default {
  name: "search",
  data() {
    return {
      value: "",
      loading: false,
      searchResult: false,
      searchList: {},
    };
  },
  computed: {
    isNull() {
      const { accountList, groupList, userGroupList } = this.searchList;
      return (
        (accountList && accountList.length === 0) ||
        (!accountList && groupList && groupList.length === 0) ||
        (!groupList && userGroupList && userGroupList.length === 0) ||
        !userGroupList
      );
    },
  },
  methods: {
    handleSearchByRealName() {
      if (!this.value) {
        this.searchList = {};
        return;
      }
      this.loading = true;
      this.$Debounce(async () => {
        if (!this.value) {
          this.searchList = {};
          this.loading = false;
          return;
        }
        const result = await this.$API.admins.searchByRealName({
          realName: this.value,
        });
        this.loading = false;
        if (result.code === 200) {
          this.searchList = result.result;
          this.searchResult = true;
        }
      }, 500)();
    },
    handleSelect(item, type) {
      this.$emit("select", { item, type });
      this.searchResult = false;
    },
    handleBlur() {
      setTimeout(() => {
        this.searchResult = false;
        this.value = "";
        this.searchList = {};
      }, 500);
    },
  },
};
</script>

<style scoped lang="less">
.sidebar-msg-fixtop {
  padding: 0 10px;
  height: 60px;
  display: flex;
  align-items: center;
  position: relative;
  background-color: #fff;
  white-space: inherit;
  .el-icon-loading {
    position: absolute;
    left: 0;
    top: 100px;
    right: 0;
    margin: auto;
    width: 20px;
    height: 20px;
    font-size: 20px;
  }
  .search-list {
    background: #fff;
    position: absolute;
    z-index: 9999;
    width: 249px;
    height: 0;
    top: 60px;
    left: 0;
    overflow: auto;
    box-sizing: border-box;
    transition: 0.3s;
    // @media screen and (min-width: 0px) and (min-width: 450px) {
    //   height: 565px;
    // }
    // @media screen and (min-width: 450px) and (min-width: 720px) {
    //   height: 690px;
    // }
    // @media screen and (min-width: 500px) and (min-width: 450px) {
    //   height: 409px;
    // }
    // @media screen and (min-width: 585px) and (min-width: 535px) {
    //   height: 472px;
    // }
    // @media screen and (min-width: 683px) and (min-width: 625px) {
    //   height: 565px;
    // }
    // @media screen and (min-width: 820px) and (min-width: 750px) {
    //   height: 690px;
    // }
    // @media screen and (min-width: 720px) and (min-width: 900px) {
    //   height: 875px;
    // }
    // @media screen and (min-width: 900px) and (max-width: 1280px) {
    //   height: 790px;
    // }
    // @media screen and (min-width: 1280px) and (max-width: 1920px) {
    //   height: 875px;
    // }
    .is-null {
      position: absolute;
      width: 100%;
      text-align: center;
      top: 200px;
      left: 0;
    }
    .search-list-title {
      color: #666;
      padding: 5px 10px;
    }
    .search-list-item {
      display: flex;
      line-height: 40px;
      padding: 5px 10px;
      cursor: pointer;
      &:hover {
        background-color: #e3e3e3;
      }
      .item-avatar {
        width: 40px;
        height: 40px;

        text-align: center;
        color: #fff;
        background: url(../assets/head.png);
        background-size: cover;
        margin-right: 10px;
      }
    }
  }
  .el-input__inner {
    background: #f8f8f9;
    border: 1px solid #ebebf2;
    border-radius: 2px;
  }
}
</style>